% IMPORTS FILES IN CURRENT DIRECTORY

files = dir('*.asc');  % lists all .asc files in directory
ertrial = [];

minl = inf;
dat = cell(1,numel(files));

for i = 1:numel(files)
    [dat{i}, trialsummary(i)] = read_trace(files(i).name);
    minl  = min(minl, size(dat{i}, 1));
end

% mdat = 0;
% for i = 1:numel(dat)
%     mdat = mdat+dat{i}(1:minl, 2:4);
% end
% mdat = mdat/numel(dat);

%  ADD NaN FOR MISSING ROWS (BLINKS) AND AT END OF TRACE

for i = 1:numel(files)

ms = size((dat{1,i}),1);
dat{1,i}((ms+1):18000,1:4) = NaN;

    for ln = 1:(ms-1)
        
        if dat{1,i}(ln+1,1) - dat{1,i}(ln,1) > 1
            % find missing time points
            missing = dat{1,i}(ln+1,1) - dat{1,i}(ln,1);
            % move section afer mising points down to correct time point
            dat{1,i}((ln+missing):(ms+missing-1),:) = dat{1,i}((ln+1):ms,:);
            % X, Y and P during blink are NaN
            dat{1,i}((ln+1):(ln+missing-1),2:4) = NaN;
            % missing milliseconds are filled in
            dat{1,i}((ln+1):(ln+missing-1),1) = ...
                (dat{1,i}(ln,1) + 1):(dat{1,i}(ln,1) + missing - 1);
            % sac (0 or 1) during blink = sac when blink started
            dat{1,i}((ln+1):(ln+missing-1),5) = dat{1,i}(ln,5);
        end
    end
end


% IMPORT DATA INTO TRIALSUMMARY STRUCTURE

for i = 1:numel(files)
    try
    trialsummary(1,i).data.time = dat{1,i}(:,1);
    trialsummary(1,i).data.X = dat{1,i}(:,2);
    trialsummary(1,i).data.Y = dat{1,i}(:,3);
    trialsummary(1,i).data.P = dat{1,i}(:,4);
    trialsummary(1,i).data.sac = dat{1,i}(:,5);
    catch
        ertrial(length(ertrial)+1) = i;
        str = ['trialsummary.data.X problem, files number: ', num2str(i)];
        disp(str);
    end
end

for i = 1:numel(files)
    try
    % CENTRE THE DATA (Make midpoint of X axis (scnw) = 0)
    trialsummary(1,i).data.X = trialsummary(1,i).data.X - 0.5*scnw;

    % FLIP TRACES SO ALL START AT SAME FIXATION POINT
    trialsummary(1,i).data.X = sign(trialsummary(1,i).fixation)* ...
        trialsummary(1,i).data.X;
    catch
        ertrial(length(ertrial)+1) = i;
        str = ['trialsummary.data.X problem, files number: ', num2str(i)];
        disp(str);
    end
    
    % RELABEL TIMES STARTING AT 1 MS
    trialsummary(1,i).data.time(18000,1) = 0;
    try
        trialsummary(1,i).data.time(:,1)     = 1:18000;
    catch
        ertrial(length(ertrial)+1) = i;
        str = ['trial >18s long, files number: ', num2str(i)];
        disp(str);
    end
    
    % ESTABLISH NO OF TRIALS PER BLOCK (IN NEXT SECTION)
    trials(1,i) = trialsummary(1,i).trial;
    trials(2,i) = trialsummary(1,i).block;
    try
        trials(3,i) = trialsummary(1,i).experiment;
    end
end


% ENSURE TRIALS ARE LISTED IN CORRECT ORDER WITHIN THE TRIALSUMMARY STRUCT

tbe = max(trials,[],2); % lists no. of trials, blocks, exps

% duplicate data 
trialsummary(2,:) = trialsummary(1,:);

% move trials from row 2 into correct position in row 1
for i = 1:numel(files)
    try
        if tbe(3) > 1  % if there are exps 1 and 2 (old version)
            j = (trialsummary(2,i).experiment-1)*tbe(2)*tbe(1) + ...
                (trialsummary(2,i).block-1)*tbe(1) + trialsummary(2,i).trial;
            trialsummary(1,j) = trialsummary(2,i);
        end
        
    catch           % if new version
        j = (trialsummary(2,i).block-1)*tbe(1) + trialsummary(2,i).trial;
        trialsummary(1,j) = trialsummary(2,i);
    end
end

% check there are no missing trials otherwise trials in row 1 will not 
% have been overwritten (only for version WITHOUT experiments 1 & 2)

if length(tbe) == 2
    for i = tbe(1):tbe(1):(tbe(1)*tbe(2))
        % check the last trial in each block is the correct number
        try
            if trialsummary(1,i).trial ~= tbe(1)
                disp(['trial missing from block ending with trial number ' num2str(i)])
            end
        catch
            disp(['problem reading trial no. data for trial number ' num2str(i)])
        end
    end
end

% remove duplicate row
trialsummary(2,:) = [];

